package com.cams.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cams.backend.common.lang.Result;
import com.cams.backend.entity.Leaves;
import com.cams.backend.entity.Record;
import com.cams.backend.entity.Work;
import com.cams.backend.mapper.LeavesMapper;
import com.cams.backend.mapper.RecordMapper;
import com.cams.backend.service.LeavesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cams
 * @since 2022-05-20
 */
@Service
public class LeavesServiceImpl extends ServiceImpl<LeavesMapper, Leaves> implements LeavesService {
    @Resource
    LeavesMapper leavesMapper;

    @Resource
    RecordMapper recordMapper;

    @Override
    public Result apply(Leaves leaves) {
        leaves.setLstate("待审批");

        Leaves temp1 = leavesMapper.selectOne(new QueryWrapper<Leaves>()
                .nested(i->i.between("lsdate",leaves.getLsdate(),leaves.getLedate()).or().between("ledate",leaves.getLsdate(),leaves.getLedate()))
                .and(i->i.eq("uid",leaves.getUid()))
        );
        Leaves temp2 = leavesMapper.selectOne(new QueryWrapper<Leaves>()
                .nested(i->i.eq("uid",leaves.getUid())
                        .le("lsdate",leaves.getLsdate())
                        .ge("ledate",leaves.getLedate()))

        );


        if(temp1!=null || temp2!=null){
            return Result.fail("该时间段内已请假！");
        }
        else{
            leavesMapper.insert(leaves);
            return Result.succeed(leaves);

        }

    }

    @Override
    public Result approve(Leaves leaves) {
        String state =leaves.getLstate();
        if(state.equals("通过")){
            LocalDate leaveDate=leaves.getLsdate();
            while(leaveDate.isBefore(leaves.getLedate()) || leaveDate.isEqual(leaves.getLedate())) {
                int year = leaveDate.getYear();
                int month = leaveDate.getMonthValue();
                int date = leaveDate.getDayOfMonth();
                LocalDateTime leaveTime = LocalDateTime.of(year,month,date,12,00,00);
                Record leaveRecord = new Record();
                leaveRecord.setUid(leaves.getUid());
                leaveRecord.setUname(leaves.getUname());
                leaveRecord.setRstate("请假");
                leaveRecord.setRtype("请假");
                leaveRecord.setRtime(leaveTime);
                recordMapper.insert(leaveRecord);
                leaveDate = leaveDate.plusDays(1);

            }
        }

        leavesMapper.updateById(leaves);
        return Result.succeed(leaves);
    }

    @Override
    public Result leaveCheckAll() {
        QueryWrapper<Leaves> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("lstate", "待审批");
        List<Leaves> leavesList = leavesMapper.selectList(queryWrapper);
        return Result.succeed(leavesList);
    }

    @Override
    public Result leaveCheckByUid(int uid) {
        QueryWrapper<Leaves> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("uid", uid);
        List<Leaves> leavesList = leavesMapper.selectList(queryWrapper);
        return Result.succeed(leavesList);
    }

    @Override
    public Result deleteLeaveByLid(int lid) {
        QueryWrapper<Leaves> queryWrapper=new QueryWrapper<Leaves>();
        queryWrapper.eq("lid",lid);
        Leaves leaves=leavesMapper.selectOne(queryWrapper);

        LocalDate leaveDate=leaves.getLsdate();

        while(leaveDate.isBefore(leaves.getLedate()) || leaveDate.isEqual(leaves.getLedate())) {
            int year = leaveDate.getYear();
            int month = leaveDate.getMonthValue();
            int date = leaveDate.getDayOfMonth();
            LocalDateTime leaveTime = LocalDateTime.of(year,month,date,12,00,00);

            QueryWrapper<Record> qw=new QueryWrapper<Record>();
            qw
                    .eq("uid",leaves.getUid())
                    .eq("rtime",leaveTime)
            ;
            recordMapper.delete(qw);
            leaveDate = leaveDate.plusDays(1);

        }

        leavesMapper.delete(queryWrapper);

        return Result.succeed(null);
    }

}
